import Taro from '@tarojs/taro';
import { observable } from 'mobx';

/**
 * 注意，common不可调用任意api，否则造成引用死循环
 */
const WX_CODE = 'wxCode';
const TOKEN = 'token';

const commonStore = observable({
  counter: 3,
  navHeight: 44,
  statusBarHeight: 0,
  specMode: false,
  category: {},
  wxCode: Taro.getStorageSync(WX_CODE) || null,
  token: Taro.getStorageSync(TOKEN) || null,

  setWxCode(wxCode: string) {
    this.wxCode = wxCode;
    Taro.setStorageSync(WX_CODE, wxCode);
  },

  setToken(token: string) {
    this.token = token;
    Taro.setStorageSync(TOKEN, token);
  },

  header() {
    let param: any = {};
    if (this.wxCode) {
      param.wxCode = this.wxCode;
    }
    if (this.token) {
      param.token = this.token;
    }
    return param;
  },
});

export default commonStore;
